﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using XueMeng.BLL;
using XueMeng.Model;

namespace UI
{
    public partial class EmployeeMain : Form
    {
        public EmployeeMain()
        {
            InitializeComponent();
        }

        private void EmployeeMain_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            LoadData();
            LoadEvent();
        }

        private void LoadData()
        {
            LoadPost();
            LoadEmp("");
        }

        /// <summary>
        /// 加载职位
        /// </summary>
        private void LoadPost()
        {
            PostManager pm = new PostManager();
            List<Post> list = pm.GetModelList("");
            list.Insert(0, new Post() { P_Id = 0, P_Name = "全部" });
            this.comboBox1.DataSource = list;
            this.comboBox1.DisplayMember = "P_Name";
            this.comboBox1.ValueMember = "P_Id";
        }

        private void LoadEvent()
        {
            //职位
            this.comboBox1.SelectedValueChanged += new EventHandler(comboBox1_SelectedValueChanged);
            //查询
            this.button1.Click += new EventHandler(button1_Click);
            //新增
            this.button2.Click += new EventHandler(button2_Click);

            //首页
            this.button3.Click += new EventHandler(button3_Click);
        }

        void button1_Click(object sender, EventArgs e)
        {
            String str = this.textBox1.Text.Trim();
            LoadEmp("E_Name like '%" + str + "%'");
        }

        void button3_Click(object sender, EventArgs e)
        {
            
        }

        void button2_Click(object sender, EventArgs e)
        {
            EmpEdit ee = new EmpEdit();
            ee.Tag = null;
            ee.StartPosition = FormStartPosition.CenterScreen;
            ee.ShowDialog();
            LoadEmp("");
        }

        void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedItem==null)
            {
                return;
            }
            int P_Id = Convert.ToInt32(this.comboBox1.SelectedValue);
            LoadEmp(P_Id>0?"P_Id = "+P_Id:"");
        }

        private void LoadEmp(String Where)
        {
            EmployeeManager em = new EmployeeManager();
            this.dataGridView1.DataSource = em.GetList(Where).Tables[0];
            //EditGridView();
        }

    }
}
